package com.lenovo.vod.player;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.lenovo.vod.model.Program;
import com.lenovo.vod.model.VideoClip;

import android.os.Bundle;
import android.util.Log;

public class PlayerTest
{
	private SAXReader reader = new SAXReader();
	private Document  doc    = null;
	
	private PlayerTest()
	{
		try 
		{
			File urlFile = new File("/data/test_url");
			
			if(urlFile.exists())
			{
				if(urlFile.canRead())
				{
					doc = reader.read(urlFile);
				}
			}
			
		}
		catch (Exception e) 
		{
			Log.i("PlayerTest", "open xml file error : " + e.getMessage());
			return;
		}
	}
	
	private static PlayerTest INSTANCE = new PlayerTest();
	
	public static PlayerTest getInstance()
	{
		return INSTANCE;
	}
	
	public Bundle getParameter(Bundle bundleData)
	{
		return this.buildBCTIPlayerBundle(bundleData, this.getBCTIProgramParameter());
	}
	
	private Bundle buildBCTIPlayerBundle(Bundle playerBundle, Program program)
	{
		if(program == null)
		{
			return null;
		}
		
		playerBundle.putInt("PlayEpisode", 0);
		playerBundle.putInt("ItemType", program.getItemType());
		playerBundle.putString("Code", program.getCode());	
		playerBundle.putInt("Type", program.getType());
		playerBundle.putString("Name", program.getName());					
		playerBundle.putString("PosterUrl", program.getBigImage2());	
		playerBundle.putString("CategoryName", program.getCategory());
		
		Bundle relateBundle=new Bundle();
		relateBundle.putString("Director", program.getDirector());
		relateBundle.putString("Actor", program.getActor());
		relateBundle.putString("Type", program.getRegion());
		
		//System.out.println("Director............"+relateBundle.getString("Director").equals(""));
		
		playerBundle.putBundle("Relate", relateBundle);
		
		Bundle episodeDetailBundle=new Bundle();
		
		if(program.getEpisodeNum()==1)
		{
			for(Map.Entry<String, VideoClip> entry: program.getVideoClipMap().entrySet())
			{
				VideoClip vc=entry.getValue();			
				Bundle bundle=new Bundle();
				bundle.putString("VideoCode",vc.getVideoCode());
				bundle.putInt("LastExitTime", 0);																				
				episodeDetailBundle.putBundle("1", bundle);		
			}
		}
		else if(program.getEpisodeNum()>1)
		{		
			for(Map.Entry<String, VideoClip> entry: program.getVideoClipMap().entrySet())
			{
				String key=entry.getKey();
				VideoClip vc=entry.getValue();			
				Bundle bundle=new Bundle();
				bundle.putString("VideoCode",vc.getVideoCode());
				bundle.putInt("LastExitTime", 0);																				
				episodeDetailBundle.putBundle(key, bundle);		
			}			
		}
		else	
		{
			System.out.println("Error:episode num error");
		}	
		
		playerBundle.putBundle("EpisodeDetail", episodeDetailBundle);
		
		return playerBundle;
	}
	
	private Program getBCTIProgramParameter()
	{
		Program result = new Program();
		
		try
		{
			Element program     = doc.getRootElement();
//			Element program  = root.element("Program");
			Element vidclips = program.element("VideoClips");
					
			@SuppressWarnings("unchecked")
			List<Element>              clipList      = vidclips.elements("VideoClip");
			HashMap<String, VideoClip> videoClipList = new LinkedHashMap<String, VideoClip>();
					
			if(clipList != null && clipList.size() > 0)
			{
				int size = clipList.size();
						
				for(int j = 0; j < size; j++)
				{
					Element clip = clipList.get(j);
					
					VideoClip videoClip = new VideoClip();
					
					try
					{
						videoClip.setAudioType(Integer.valueOf(clip.attributeValue("AudioType")));
					}
					catch(Exception e)
					{
						videoClip.setAudioType(0);
					}
					
					try
					{
						videoClip.setBitRateType(Integer.valueOf(clip.attributeValue("BitRateType")));
					}
					catch(Exception e)
					{
						videoClip.setBitRateType(0);
					}
					
					try
					{
						videoClip.setCanDowload(Integer.valueOf(clip.attributeValue("CanDownload")));
					}
					catch(Exception e)
					{
						videoClip.setCanDowload(0);
					}
					
					try
					{
						videoClip.setCanOnlinePlay(Integer.valueOf(clip.attributeValue("CanOnlinePlay")));
					}
					catch(Exception e)
					{
						videoClip.setCanOnlinePlay(0);
					}
					
					try
					{
						videoClip.setCaptioning(Integer.valueOf(clip.attributeValue("Captioning")));
					}
					catch(Exception e)
					{
						videoClip.setCaptioning(0);
					}
					
					try
					{
						videoClip.setDuration(Integer.valueOf(clip.attributeValue("Duration")));
					}
					catch(Exception e)
					{
						videoClip.setDuration(0);
					}
					
					try
					{
						videoClip.setEpisodeIndex(Integer.valueOf(clip.attributeValue("EpisodeIndex")));
					}
					catch(Exception e)
					{
						videoClip.setEpisodeIndex(0);
					}
					
					try
					{
						videoClip.setFileSize(Integer.valueOf(clip.attributeValue("FileSize")));
					}
					catch(Exception e)
					{
						videoClip.setFileSize(0);
					}
					
					try
					{
						videoClip.setFormat3d(Integer.valueOf(clip.attributeValue("Format3D")));
					}
					catch(Exception e)
					{
						videoClip.setFormat3d(0);
					}
					
					try
					{
						videoClip.setIs3dVideo(Integer.valueOf(clip.attributeValue("Is3dvideo")));
					}
					catch(Exception e)
					{
						videoClip.setIs3dVideo(0);
					}
					
					try
					{
						videoClip.setIsDrm(Integer.valueOf(clip.attributeValue("IsDRM")));
					}
					catch(Exception e)
					{
						videoClip.setIsDrm(0);
					}
					
					try
					{
						videoClip.setPrice(Integer.valueOf(clip.attributeValue("Price")));
					}
					catch(Exception e)
					{
						videoClip.setPrice(0);
					}
					
					try
					{
						videoClip.setResolution(Integer.valueOf(clip.attributeValue("Resolution")));
					}
					catch(Exception e)
					{
						videoClip.setResolution(0);
					}
					
					videoClip.setOrderCode(clip.attributeValue("OrderCode"));
					
					try
					{
						videoClip.setScreenFormat(Integer.valueOf(clip.attributeValue("ScreenFormat")));
					}
					catch(Exception e)
					{
						videoClip.setScreenFormat(0);
					}
					
					try
					{
						videoClip.setVideoType(Integer.valueOf(clip.attributeValue("VideoType")));
					}
					catch(Exception e)
					{
						videoClip.setVideoType(0);
					}
					
					
					videoClip.setType(clip.attributeValue("Type"));
					videoClip.setVideoCode(clip.attributeValue("VideoCode"));
					videoClipList.put(String.valueOf(j), videoClip);
				}
				
				result.setVideoClipMap(videoClipList);
			}
			
			try
			{
				result.setLength(Integer.valueOf(program.attributeValue("Length")));
			}
			catch(Exception e)
			{
				result.setLength(0);
			}
			
			try
			{
				result.setType(Integer.valueOf(program.attributeValue("Type")));
			}
			catch(Exception e)
			{
				result.setType(0);
			}
			
			try
			{
				result.setEpisodeNum(Integer.valueOf(program.attributeValue("EpisodeNum")));
			}
			catch(Exception e)
			{
				result.setEpisodeNum(0);
			}
			
			try
			{
				result.setRatingLevel(Float.valueOf(program.attributeValue("RatingLevel")));
			}
			catch(Exception e)
			{
				result.setRatingLevel(0);
			}
			
			result.setActor(program.attributeValue("Actor"));
			result.setBigImage1(program.attributeValue("BigImage1"));	
			result.setBigImage2(program.attributeValue("BigImage2"));	
			result.setCode(program.attributeValue("Code"));
			result.setName(program.attributeValue("Name"));
			result.setDirector(program.attributeValue("Director"));
			result.setRegion(program.attributeValue("Region"));
			result.setLanguage(program.attributeValue("Language"));
			result.setProgramType(program.attributeValue("ProgramType"));
			result.setKeyWords(program.attributeValue("Keywords"));
			result.setDesc(program.attributeValue("Desc"));
			result.setStatus(program.attributeValue("Status"));
			result.setSmallImage1(program.attributeValue("SmallImage1"));
			result.setSmallImage2(program.attributeValue("SmallImage2"));
			
			// ItemType default value is 1
			result.setItemType(1);
			
			return result;
		}
		catch(Exception e)
		{
			Log.i("PlayerTest", "parse xml file error : " + e.getMessage());
			return null;
		}
	}
}
